package com.navi168.fwq21.hyzl;

import java.util.ArrayList;
import com.navi168.fwq21.R;
import com.navi168.fwq21.db.Sqlh;
import com.navi168.fwq21.hyzl.SendList.ViewHolder;
import com.navi168.fwq21.hyzl.mod.Favo;
import com.navi168.fwq21.hyzl.mod.MypoiItems;
import com.navi168.fwq21.hyzl.mod.Xfhistroy;
import com.navi168.fwq21.util.Tools;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SomeList extends TabActivity {
	private TextView adrname1;
	private TextView adrname2;
	private ListView favlistView;
	private ListView hislistView;
	private ListView hislistView2;
	private ListView meslistView;
	private ListView meslistView2;
	private FavoAdapter favoAdapter;
	private HisAdapter hisAdapter;
	private MypoiItems mpiItems = new MypoiItems();
	private ArrayList<Favo> favos;
	private ArrayList<Xfhistroy> xfhistroys;
	private Sqlh sqlh;
	private Context context;
	private String[] favomenu_arry = { "删除兴趣点", "地图查看", "下发地址" };
	private String[] hismenu_arry = { "查看详细", "加入兴趣点", "地图查看", "发送地址", "删除记录" };
	
	TabHost tabhost;

	private int width;

	// private int height;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.somelist);
		sqlh = sqlh.getInstance(this);
		favos = sqlh.getFavcursor();
		xfhistroys = sqlh.getxfhList();
		context = getApplicationContext();
		Log.i("mylog", "here 4");

		DisplayMetrics dm = new DisplayMetrics();
		width = dm.widthPixels;
		// height = dm.heightPixels;
		tabhost = getTabHost();
		tabhost.addTab(tabhost.newTabSpec("favolist").setIndicator("兴趣点")
				.setContent(R.id.favlist));
		tabhost.addTab(tabhost.newTabSpec("hislist").setIndicator("下发历史")
				.setContent(R.id.hislist));
		tabhost.addTab(tabhost.newTabSpec("meslist").setIndicator("消息历史")
				.setContent(R.id.meslist));

		final TabWidget tabwidget = tabhost.getTabWidget();
		Log.i("mylog", "here 3");
		for (int i = 0; i < tabwidget.getChildCount(); i++) {
			// 设置页签高度和页签内字体属性
			TextView tv = (TextView) tabwidget.getChildAt(i).findViewById(
					android.R.id.title);
			tabwidget.getChildAt(i).getLayoutParams().height = 60;
			tabwidget.getChildAt(i).getLayoutParams().width = width / 3;
			// tabwidget.setBackgroundResource(R.drawable.toolbar);
			tv.setTextSize(15);
			tv.setTextColor(Color.WHITE);
			tv.setGravity(Gravity.CENTER_HORIZONTAL);
		}
		findviews();
		setlisters();
	}

	private void setlisters() {
		tabhost.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				favos = sqlh.getFavcursor();
				favoAdapter.notifyDataSetChanged();
				
			}
		});
		
		favlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					final int arg2, long arg3) {

				AlertDialog.Builder builder = new Builder(SomeList.this);
				builder.setItems(favomenu_arry,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Favo favo = favos.get(arg2);
								switch (which) {
								case 0:// 删除记录
									int a = delrecord(favos.get(arg2).getId(),
											"fav_list");
									if (a > 0) {
										showToast("此记录已删除！");
										favos = sqlh.getFavcursor();
										favoAdapter.notifyDataSetChanged();
									} else {
										showToast("无法删除！");
									}
									break;
								case 1:// 在地图上查看

									sendpoiitem(favo.getId(),
											favo.getFav_snipeet(),
											favo.getFav_title(),
											favo.getFav_lon(),
											favo.getFav_lat(), 1);
									break;

								case 2:// 下发地址

									mpiItems.setLatit(favo.getFav_lat());
									mpiItems.setLongit(favo.getFav_lon());
									mpiItems.setSnippet(favo.getFav_snipeet());
									mpiItems.setTitle(favo.getFav_title());
									Bundle bundle = new Bundle();
									bundle.putParcelable("poi_xf", mpiItems);
									Intent it = new Intent(SomeList.this,
											SendList.class);
									it.putExtra("poi_xf", bundle);
									startActivity(it);

									break;
								default:
									break;
								}

							}

						});
				builder.create().show();

			}

		});

		hislistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					final int position, long id) {
				AlertDialog.Builder builder = new Builder(SomeList.this);
				builder.setItems(hismenu_arry,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Xfhistroy xfh = xfhistroys.get(position);
								switch (which) {
								case 0:// 查看详细

									break;
								case 1:// 加入兴趣点
									Favo favo = new Favo();
									favo.setFav_lat(xfh.getHistroy_lat());
									favo.setFav_lon(xfh.getHistroy_long());
									favo.setFav_snipeet(xfh
											.getHistroy_snippet());
									favo.setFav_title(xfh.getHistroy_title());
									favo.setJoin_date(new Tools().getnowtime());
									int f = sqlh.add(favo);
									if (f > 0) {
										showToast("加入兴趣点成功！");
									} else {
										showToast("无法加入，请稍后再试！");
									}

									break;
								case 2:// 查看地图
									sendpoiitem(xfh.getHistroy_id(),
											xfh.getHistroy_snippet(),
											xfh.getHistroy_title(),
											xfh.getHistroy_long(),
											xfh.getHistroy_lat(), 1);
									break;
								case 3:// 发送地址
									mpiItems.setLatit(xfh.getHistroy_lat());
									mpiItems.setLongit(xfh.getHistroy_long());
									mpiItems.setSnippet(xfh.getHistroy_snippet());
									mpiItems.setTitle(xfh.getHistroy_title());
									Bundle bundle = new Bundle();
									bundle.putParcelable("poi_xf", mpiItems);
									Intent it = new Intent(SomeList.this,
											SendList.class);
									it.putExtra("poi_xf", bundle);
									startActivity(it);
									break;
								case 4:// 删除记录
									int a = delrecord(xfh.getHistroy_id(), "histroylist");
									if (a > 0) {
										showToast("此记录已删除！");
										xfhistroys = sqlh.getxfhList();
										hisAdapter.notifyDataSetChanged();
									} else {
										showToast("无法删除，请稍候再试！");
									}
									
									break;

								default:
									break;
								}

							}
						});
				builder.create().show();

			}

		});

	}

	private void findviews() {
		favlistView = (ListView) findViewById(R.id.listview_favo);
		favoAdapter = new FavoAdapter();
		favlistView.setAdapter(favoAdapter);
		Log.i("mylog", "here 2");

		hislistView = (ListView) findViewById(R.id.listView_hisxf);
		hisAdapter = new HisAdapter();
		hislistView.setAdapter(hisAdapter);

	}

	/**
	 * @author FWQ21 兴趣列表的适配器
	 */
	public class FavoAdapter extends BaseAdapter {
		ViewHolder vHolder = null;

		@Override
		public int getCount() {
			//
			return favos.size();
		}

		@Override
		public Object getItem(int arg0) {
			//
			return favos.get(arg0);
		}

		@Override
		public long getItemId(int position) {
			//
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {

			if (convertView == null) {
				vHolder = new ViewHolder();
				LayoutInflater inflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.alllist_list_c, parent,
						false);
				vHolder.adrqc = (TextView) convertView
						.findViewById(R.id.adressname1);
				vHolder.adrjc = (TextView) convertView
						.findViewById(R.id.adressname2);

				convertView.setTag(vHolder);

			} else {
				vHolder = (ViewHolder) convertView.getTag();
			}

			vHolder.adrqc.setText(favos.get(position).getFav_snipeet());
			vHolder.adrjc.setText(favos.get(position).getFav_title());

			Log.i("mylog", "here 1");
			return convertView;
		}

	}

	/**
	 * @author FWQ21 历史记录的适配器
	 */
	public class HisAdapter extends BaseAdapter {
		ViewHolder vHolder = null;

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return xfhistroys.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return xfhistroys.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				vHolder = new ViewHolder();
				LayoutInflater inflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.alllist_list_c, parent,
						false);
				vHolder.adrqc = (TextView) convertView
						.findViewById(R.id.adressname1);
				vHolder.adrjc = (TextView) convertView
						.findViewById(R.id.adressname2);

				convertView.setTag(vHolder);

			} else {
				vHolder = (ViewHolder) convertView.getTag();
			}

			vHolder.adrqc
					.setText(xfhistroys.get(position).getHistroy_snippet());
			vHolder.adrjc.setText(xfhistroys.get(position).getHistroy_title());

			Log.i("mylog", "here 1");
			return convertView;
		}
	}

	public class ViewHolder {

		public TextView adrqc;
		public TextView adrjc;

	}

	/**
	 * 
	 * 功能：删除某张表中的记录
	 * @param id 记录id
	 * @param tablename 表名称
	 * @return 返回操作影响的行数
	 * int
	 */
	public int delrecord(int id, String tablename) {

		return sqlh.del(id, tablename);

	}

	private void showToast(String showString) {
		Toast.makeText(getApplicationContext(), showString, Toast.LENGTH_SHORT)
				.show();
	}

	private void sendpoiitem(int id, String adrqc, String adrjc, int lon,
			int lat, int requestcode) {
		// MypoiItems mpiItems = new MypoiItems();
		mpiItems.setLatit(lat);
		mpiItems.setLongit(lon);
		mpiItems.setTitle(adrqc);
		mpiItems.setSnippet(adrjc);
		Intent intent = new Intent(SomeList.this, ShowMap.class);
		intent.putExtra("poiitem", mpiItems);
		setResult(requestcode, intent);
		SomeList.this.finish();

	}

}
